package buttons.control;

import processing.core.PApplet;
import buttons.PopUpMenu;
import buttons.view.V_DropDown;

public class C_DropDown extends C_Button // implements EventListener
{
  private boolean expanded;
  private String cachedOption = "";
  private PopUpMenu options;
  
  public C_DropDown(V_DropDown viewButton, PApplet p){
    super(viewButton);
    initOptions(viewButton, p);
  }
  private void initOptions(V_DropDown viewButton, PApplet p){
    options = new PopUpMenu(p);
    viewButton.addOptions(options.getView());
  }
  
  @Override
  public void onInteract (float mouseXLoc, float mouseYLoc, String interID)
  {
     V_DropDown button = (V_DropDown)vFeature;
     boolean mouseOver = button.contains(mouseXLoc, mouseYLoc);
     if (expanded == true) {
        options.getController().setActive(true);
        options.onInteract(mouseXLoc, mouseYLoc, interID);
        if(interID.equals("leftClick")) {
          expanded = false;
          button.setOffset(0);
          String selected = options.getController().getHighlightedOption(); //check the value of the option:
          if(selected.equals("") == false && selected.equals(button.getCurOption()) == false) {
            button.setCurOption(selected);
            triggerEvent(selected);
          }
          // turn options off:
          options.getController().setActive(false);
          options.onInteract(mouseXLoc, mouseYLoc, interID); // turns off menu
        }
      } 
      else if(mouseOver)
      {
          if(interID.equals("leftClick"))
          {
            button.setOffset(-30);
            expanded = true;
          }
          else // if mouse over...
          {
              // set the background to an "over" color
              button.setOffset(-10);
              // start a tooltip counter
              handleTooltip(mouseXLoc, mouseYLoc);
          }          
      }
      else // if mouse not over the button
      { 
        button.setOffset(0);
        //if(v_toolTip.getText().equals(tooltip)) c_toolTip.setActive(false);
      }
  }
  
  public void addOption(String item)
  {
    options.addItem(item);
  }
  public void cacheCurOption()  {
    cachedOption = ((V_DropDown)vFeature).getCurOption();
  }
  public String getCache() {
    return cachedOption;
  }
  public boolean isExpanded() {
    return expanded;
  }
  
  /*
  // HOLD-BASED interaction
  void onInteract (float mouseXLoc, float mouseYLoc, String interID)
  {
     V_DropDown button = (V_DropDown)vFeature;
     boolean mouseOver = button.contains(mouseXLoc, mouseYLoc);
     if (expanded == true) {
        if(interID.equals("leftDrag") || interID.equals("leftPressed")) {
          options.getController().setActive(true);
          options.onInteract(mouseXLoc, mouseYLoc, interID);
        }
        else if(interID.equals("released")) {
          expanded = false;
          button.setOffset(0);
          String selected = options.getController().getHighlightedOption(); //check the value of the option:
          if(selected.equals("") == false && selected.equals(button.getCurOption()) == false) {
            button.setCurOption(selected);
            triggerEvent(selected);
          }
          // turn options off:
          options.getController().setActive(false);
          options.onInteract(mouseXLoc, mouseYLoc, interID); // turns off menu
        }
      } 
      else if(mouseOver)
      {
          if(interID.equals("leftPressed") || interID.equals("leftDrag"))
          {
            button.setOffset(-30);
            expanded = true;
          }
          else // if mouse over...
          {
              // set the background to an "over" color
              button.setOffset(-10);
              // start a tooltip counter
              if(c_toolTip.checkForTip(mouseXLoc, mouseYLoc))
              {
                  if(tooltip.length() > 0) // if the button HAS a set tooltip display it
                  {
                    v_toolTip.setName(tooltip);
                    c_toolTip.setActive(true);
                  }
              }
              else c_toolTip.setActive(false);
          }          
      }
      else // if mouse not over the button
      { 
        button.setOffset(0);
        if(v_toolTip.getText().equals(tooltip)) c_toolTip.setActive(false);
      }
  }*/
}
